{% extends 'base_templates/_page_layout.html' %}

{% block css %}
    <style>
    </style>
    <!-- Table Responsive css -->
    <link href="/static/plugins/minton/plugins/RWD-Table-Patterns/dist/css/rwd-table.min.css" rel="stylesheet" type="text/css" media="screen">
    <!-- DataTables -->
    <link href="/static/plugins/minton/plugins/datatables/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css">
    <link href="/static/plugins/minton/plugins/datatables/buttons.bootstrap4.min.css" rel="stylesheet" type="text/css">

{% endblock %}

{% block title %}

    <!-- Page-Title -->
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <h1 class="page-title">权限管理</h1>
                <ol class="breadcrumb float-right">
                    <li class="breadcrumb-item"><a href="/permission/list">权限管理</a></li>
                    <li class="breadcrumb-item active">Dashboard</li>
                </ol>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>

{% endblock %}

{% block conent %}

<div class="row">
    <div class="col-12">
        <div class="card-box">
            <h4 class="m-t-0 header-title">权限列表</h4>
            <p class="text-muted m-b-30 font-13">
                请选择需要操作的 <code>权限</code>, 进行 <code>查看</code> 或者 <code>修改</code> 等操作.
            </p>
            
            <div class="form-row">
                <div class="form-group col-md-6">
                    
                </div>
                <div class="form-group col-md-6">
                    <div class="form-group mx-sm-3 text-right">
                        <button id="btnDeletePermissions" class="btn btn-danger"><i class="fa fa-times"></i> <span>Delete Permissions</span></button>
                        <button id="btnAddPermissions" class="btn btn-primary" data-toggle="modal" data-target="#AddPermissions"><i class="fa fa-plus m-r-5"></i> <span>Add Permissions</span></button>
                    </div>
                </div>
            </div>
            
            <table id="permissions-tables" class="table table-striped table-bordered toggle-circle m-b-0 default footable-loaded footable" data-page-size="7">
                <thead>
                    <tr>
                        <th data-toggle="true" class="footable-visible footable-first-column footable-sortable">
                            <input type="checkbox" onclick="SelectAllObjs(this)">
                        </th>
                        
                        <th data-toggle="true" class="footable-visible footable-first-column footable-sortable">
                            代码名称
                            <span class="footable-sort-indicator"></span>
                        </th>
                        <th data-toggle="true" class="footable-visible footable-first-column footable-sortable">
                            Name
                            <span class="footable-sort-indicator"></span>
                        </th>
                            
                    </tr>
                </thead>
                <tbody id="content_table">
                    
                </tbody>
                <tfoot>
                    
                </tfoot>

            </table>
        </div>
    </div>
</div>


{% include 'include/_create_new_permissions.html' %}


{% endblock %}

{% block js %}
<!-- responsive-table-->
<script src="/static/plugins/minton/plugins/RWD-Table-Patterns/dist/js/rwd-table.min.js" type="text/javascript"></script>
<script>
    $(function() {
        $('.table-responsive').responsiveTable({
            addDisplayAllBtn: 'btn btn-secondary'
        });
        bindDeletePermissions();
    });

</script>
<script>
    var status_code = "{{ status_code }}"
    if (status_code == "402") {
        sweetAlert("错误的注册", "请重新注册，填写正确的信息!", "error");
        $("#btnAddPermissions").click()
    }

    function SelectAllObjs(ele) {

        if ($(ele).prop('checked')){
            $('input[row-select]').prop('checked',true)

        }else {
            $('input[row-select]').prop('checked',false)
        }

    }


    function ActionCheck(ele){
        var selected_action = $("select[name='action']").val();
        var selected_objs = $("input[row-select]").filter(":checked");
        console.log($("select[name='action']").val())
        if (!selected_action){
            alert("no action selected!");
            return false
        }
        if (selected_objs.length == 0 ){
            alert("no object selected!");
            return false
        }else {
            //生成一个标签,放到form里

            var selected_ids = [];
            $.each(selected_objs,function () {
                console.log($(this) );
                selected_ids.push($(this).val())
            });
            console.log(selected_ids);
            var input_ele = "<input type='hidden' name='selected_ids' value=" + JSON.stringify(selected_ids) + ">";

            $(ele).append(input_ele);
        }

    }

    function bindDeletePermissions(){

        $("#btnDeletePermissions").click(function(){
            var postData = {};
            var DELETEDATA = "PermissionsList"
            postData[DELETEDATA] = [];
            $("#permissions-tables").find('input[type="checkbox"]').each(function(){
                if ($(this).prop('checked') && $(this).attr("row-select")){
                    postData[DELETEDATA].push($(this).val());
                }
            })
            
            
            if (!jQuery.isEmptyObject(postData[DELETEDATA])){
                swal({
                    title: '你确定要删除？',
                    text: "点击确定后会进行删除！",
                    type: 'question', // warning
                    showCancelButton: true,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    confirmButtonClass: 'btn btn-success mt-2 delete-resume-source',
                    cancelButtonClass: 'btn btn-danger ml-2 mt-2',
                    buttonsStyling: false
                }).then(function () {
                    
                }, function (dismiss) {
                    if (dismiss === 'cancel') {
                        swal({
                            title: '已取消',
                            text: "成功取消删除操作!",
                            type: 'success',
                            confirmButtonClass: 'btn btn-confirm mt-2'
                        })
                    }
                });
                $('.delete-resume-source').click(function () {
                    postData[DELETEDATA] = JSON.stringify(postData[DELETEDATA])
                    DeleteData(postData);
                })
            }else {
                $.Notification.autoHideNotify('error', 'top right', '无效的删除','无效的操作，没有需要删除的数据，请查看后再次删除!')
            }
            
        })
    }
    function DeleteData(postData){
        $.ajax({
            url: '/permission/delete',
            data: postData,
            type: 'POST',
            dataType: "JSON",
            success: function(arg){
                if (arg.status_code != "200"){
                    swal({
                        title: '错误',
                        text: "数据删除失败!",
                        type: 'error',
                        confirmButtonClass: 'btn btn-confirm mt-2'
                    })
                }else{
                    swal({
                        title: '已删除',
                        text: "数据删除成功!",
                        type: 'success',
                        confirmButtonClass: 'btn btn-confirm mt-2'
                    }).then(function () {
                        window.location.reload();
                    });
                    
                }
            }
        })
    }
</script>
<script>
    var d = JSON.parse('{{ permissions }}');
    function obtainPermissionList(){
            $.each(d.permissions,function (k,v) {

                var tr = document.createElement('tr');
                var checkbox_box = '<td><input row-select="true" type="checkbox" value=' + v.id + ' content_type_id=' + v.content_type_id + '></td>';
                tr.innerHTML = checkbox_box
                $.each(v,function (j,l) {
                    if(j == "codename"){
                        var td = document.createElement('td');
                        td.innerHTML = "<a href=" + v.id + "/change" + ">"+l+"</a>"
                        td.classList.add("footable-visible");
                        tr.append(td)
                    }else if(j == "name"){
                        var td = document.createElement('td');
                        td.innerHTML = l
                        td.classList.add("footable-visible");
                        tr.append(td)
                    }
                });
                $("#content_table").append(tr)
            })
    }
    $(function() {
        obtainPermissionList();

    });
</script>

<script type="text/javascript">
    $(document).ready(function() {

        // Default Datatable
        $('#datatable').DataTable();

        $("#permissions-tables").DataTable({
            select: {
                style: 'multi'
            },
            lengthChange: false,
        });

        //Buttons examples
        var table = $('#datatable-buttons').DataTable({
            lengthChange: false,
            buttons: ['copy', 'excel', 'pdf']
        });

        // Key Tables

        $('#key-table').DataTable({
            keys: true
        });

        // Responsive Datatable
        $('#responsive-datatable').DataTable();

        // Multi Selection Datatable
        $('#selection-datatable').DataTable({
            select: {
                style: 'multi'
            }
        });

        table.buttons().container()
                .appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
    } );

</script>
{% endblock %}